;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.PackageSpec", ['ngImgCrop'])
        .controller("PackageSpecCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "EventBus", "UserContextService", "Upload", "ybConfig", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, EventBus, UserContextService, Upload, ybConfig) {

            $scope.activeFlag = [true, false, false, false];
            $scope.detailFlag = [true, true];
            $scope.markInfo = {
                allChecked: false,
                curMaterialRow: -1,
                curLocationRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.locationPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.detailInfo = {};
            $scope.materialsTypeList = [];
            $scope.packageMethodList = [];
            $scope.materialInfo = {};
            $scope.custodianList = [];
            $scope.locationList = [];
            $scope.materielClass = {};
            $scope.salePriceName = "进货基准价";
            $scope.tableConfig = {
                fixedWidth: 80,
                scrollWidth: 1200,
                tableCol: [
                    //xbt
                    {
                        type: "",
                        name: "包装箱编码",
                        dataField: "packageNo",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                    },
                    {
                        type: "",
                        name: "包装箱名称",
                        dataField: "packageName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "规格型号（长*宽*高）",
                        dataField: "spec",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "成本费用",
                        dataField: "cost",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "重量",
                        dataField: "weight",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "包装方式",
                        dataField: "packageMethodName",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "绑定零件编号",
                        dataField: "materialsNo",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "创建人姓名",
                        dataField: "createName",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createTime",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width:80,
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };


            var btnPermissionCode = {
                packageAdd: window.ActionCode.packageAdd,
                packageEdit: window.ActionCode.packageEdit,
            };

            $scope.btnPermission = {
                packageAdd: true,
                packageEdit: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }


            $scope.setFixedWidth = function () {
                $scope.tbStyle = { scrollLeft: { left: $scope.tableConfig.fixedWidth + "px" } };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            };


            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {
                };
                $scope.detailFlag = [true, true];
            }

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curMaterialRow = -1;
                // $scope.exportParam.sectionParam.materialsIds = [];
                var service = "device";
                var reqUrl = "/packageSpec/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.pageInfoList, function (item) {
                        item.createTime = $filter("YbDate")(item.createTime, 'yyyy-MM-dd HH:mm:ss');
                        item.packageMethodName = $filter("packageMethod")(item.packageMethod);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.pageInfoList;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }

            };

            $scope.selectRow = function (index, type) {
                $scope.markInfo["cur" + type + "Row"] = index;
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.switchDetailBar = function (index) {
                $scope.detailFlag[index] = !$scope.detailFlag[index];
            };

            $scope.addData = function () {
                $scope.saveEditType = "add";
                $scope.selectedMaterialList.materialList = [];
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                getPackageMethod();
                $scope.changeTab(1);
            };
            function getPackageMethod() {
                var service = "device";
                var reqUrl = "/dataDictionary/searchList";
                var param = {
                    parentCode:"ZXLX"
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.packageMethodList = resp.value;
                });
            }


            $scope.specList =[];

            $scope.saveEditType = "";
            $scope.editData = function (item) {
                $scope.tabFlag.name = tabFlag[1];
                $scope.detailInfo = item;
                getPackageMethod();
                $scope.specList = item.spec.split("*")
                $scope.detailInfo.specLong = $scope.specList[0]
                $scope.detailInfo.specWidth = $scope.specList[1]
                $scope.detailInfo.specHeight = $scope.specList[2];

                var service = "device";
                var reqUrl = "/materials/listForOut";
                var param = {
                    filters:{keyWords:item.materialsNo}
                };
                param.pageSize = $scope.locationPageInfo.pageSize;
                param.pageNum = $scope.locationPageInfo.curPage;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.selectedMaterialList.materialList[0] = resp.value.list[0];
                });

                $scope.saveEditType = "edit";
                $scope.changeTab(1);
            };

            function viewDetail(item) {
                $scope.detailInfo = item;
                $scope.changeTab(2);
            }


            $scope.selMateriel = function (e) {
                if (!!$scope.toolbarConfig.showMateriel) {
                    e.stopPropagation();
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig.showMateriel = true;
                        EventBus.Publish("getMaterial");
                    });
                }
            };
            $scope.selectedMaterialList = {
                materialList:[],
            };
            $scope.$watch('selectedMaterialList.materialList.length', function () {
                if($scope.selectedMaterialList.materialList.length == 1){
                    $scope.detailInfo.materialsNo = $scope.selectedMaterialList.materialList[0].materialsNo;
                }
                if($scope.selectedMaterialList.materialList.length >1){
                    ybWidget.ShowMessage("只能选择一个绑定零件", MESG_TYPE.Error);
                }
            });

            $scope.saveDataFlag = true;
            $scope.saveData = function (isValid) {
                if(!$scope.saveDataFlag){
                    return;
                }
                $scope.saveDataFlag = false;
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var param = {};
                if( $scope.saveEditType == "add"){
                    var reqUrl = "/packageSpec/add";
                }
                if( $scope.saveEditType == "edit"){
                    var reqUrl = "/packageSpec/update";
                    param.id = $scope.detailInfo.id;
                }
                $scope.detailInfo.spec =  $scope.detailInfo.specLong + "*" + $scope.detailInfo.specWidth + "*"+ $scope.detailInfo.specHeight;

                param.packageName = $scope.detailInfo.packageName;
                param.spec = $scope.detailInfo.spec;
                param.cost = $scope.detailInfo.cost;
                param.weight = $scope.detailInfo.weight;
                param.packageMethod = $scope.detailInfo.packageMethod;
                param.price = $scope.detailInfo.price;
                param.remark = $scope.detailInfo.remark;
                param.materialsNo = $scope.detailInfo.materialsNo;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.saveDataFlag = true;
                    $scope.changeTab(0);
                    $scope.detailInfo = {};
                    getDataList();
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
        ;
})(window, window.angular);